package org.gap.wizards.items.construction;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.gap.wizards.effects.Effect;
import org.gap.wizards.items.Description;
import org.gap.wizards.items.Item;
import org.gap.wizards.pc.PlayerCharacter;
import org.gap.wizards.pc.Skill;

public class ElixerInstructions extends Item implements Instructions {

	private final Item[] requiredItems;
	private final Item potion;
	private final int modifier;
	private final String requiredSkill;

	public ElixerInstructions(Description description, Item potion, String requiredSkill, int modifier, Item...items) {
		super(Effect.NONE, description);
		this.potion = potion;
		this.requiredSkill = requiredSkill;
		this.modifier = modifier;
		this.requiredItems = items;
	}

	@Override
	public boolean usableItems(Item[] itemsToCombine) {
		Set<String> listOfItems = getItemNames(itemsToCombine);  
		for (Item each : requiredItems) {
			if (!listOfItems.contains(each.getName())) {
				return false;
			}
		}
		return true;
	}
	
	public List<Item> getComponents() {
		return Arrays.asList(requiredItems);
	}

	private Set<String> getItemNames(Item[] itemsToCombine) {
		Set<String> namedItems = new HashSet<String>();
		for (Item each : itemsToCombine) {
			namedItems.add(each.getName());
		}
		return namedItems;
	}

	@Override
	public Item make(PlayerCharacter pc, Item[] itemsToCombine) {
		Skill skill = pc.getSkill(requiredSkill);
		if (skill == null) {
			return null;
		}
		switch(skill.rollWithModifier(modifier)) {
		case SUCCESS:
			return potion;
		}
		return null;
	}

}
